angular.module('starter.controllers', ['starter.controllers']) //播放指令

	.controller('playCtrl', function($scope, Chats, $stateParams, $sce, songInfo, $rootScope) {
		$scope.showHeat = false
		$scope.tabbarShow = false
		$scope.l_show = true
		$scope.l_showLrc = function() {
			$scope.l_show = !$scope.l_show
		}
		$scope.toStop = function() {
			var pause = $("#audio")[0].paused
			if(!pause) {
				$("#audio")[0].pause()
				$(".songPic_img").toggleClass("animatinParse")
				$(".songPic_img").toggleClass("animatinRuning")
				$(".playing_conter3").toggleClass("ion-ios-pause-outline")
				$(".playing_conter3").toggleClass("ion-ios-play-outline")
				$(".l_zhizhen_img").toggleClass("l_zhizhen_img_on")
				$(".l_zhizhen_img").toggleClass("l_zhizhen_img_off")
			} else {
				$("#audio")[0].play()
				$(".l_zhizhen_img").toggleClass("l_zhizhen_img_on")
				$(".l_zhizhen_img").toggleClass("l_zhizhen_img_off")
				$(".songPic_img").toggleClass("animatinParse")
				$(".songPic_img").toggleClass("animatinRuning")
				$(".playing_conter3").toggleClass("ion-ios-pause-outline")
				$(".playing_conter3").toggleClass("ion-ios-play-outline")
			}
		}
		$rootScope.pause = function() {
			var a = $("#audio")[0].pause()
		}
		$scope.goback = function() {
			window.history.back()
		}
		$('.ranges').on('input propertychange', function(ev) {
			$("#audio")[0].currentTime = $("#audio")[0].duration * (ev.target.value / 100)
		})
		$scope.id = $stateParams.id
		$scope.song = "method=baidu.ting.song.play&songid=" + $scope.id
		$scope.lrc = "method=baidu.ting.song.lry&songid=" + $scope.id
		var lrctitle = []
		var lrctime = []
		Chats.music($scope.lrc).then(function(data) {
			var lrc = data.data.lrcContent
			lrc = lrc.split('\n')
			lrctime = lrc.map(function(i) {
				lrctitle.push(i.slice(1, 2) == "0" ? i.slice(10, i.longth) || "..." : "...")
				var time = i.slice(1, 2) == "0" ? i.slice(0, 10) : ""
				time = time.slice(1, -1)
				var time_min = time.slice(0, 2)
				var time_sec = time.slice(3, time.length)
				return time_min * 60 + time_sec * 1
			})
			$scope.lrctake = lrctitle
			// console.log(lrctitle)
			// console.log(lrctime)
			//			songInfo.songPlay(lrctime,lrctitle)
		})
		var l_userdata = localStorage.getItem("myLike")
		if(l_userdata) {
			l_userdata = JSON.parse(l_userdata)
		} else {
			l_userdata = []
		}
		var l_num = l_userdata.indexOf($scope.id)
		if(l_num > 0) {
			$scope.showHeat = true
		} else {
			$scope.showHeat = false
		}
		Chats.music($scope.song).then(function(data) {
			// console.log(data)
			var name = data.data.songinfo.title
			var author = data.data.songinfo.author
			$scope.l_image_src = data.data.songinfo.pic_premium
			$scope.songPic = data.data.songinfo.pic_premium
			$scope.l_name = name
			$scope.l_author = author
			$scope.l_song_src = data.data.bitrate.file_link
			var songTime = data.data.bitrate.file_duration
			$scope.l_song_time = Math.floor(songTime / 60) + ":" + ((songTime % 60).toString().length <= 1 ? "0" + songTime % 60 : songTime % 60)
			// console.log($scope.l_song_time)
			$scope.$emit("changeFromBody",$scope.l_song_src );
			$scope.data = {
				"name": "视频",
				"url": data.data.bitrate.file_link //视频路径
			};	
			$scope.data.url = $sce.trustAsResourceUrl($scope.data.url);
			var playing = 0
			$("#audio").on("timeupdate", function(data) {
				var song_time = data.target.currentTime
				lrctime.push(song_time)
				lrctime.sort(function(a, b) { return a - b })
				var a = lrctime.indexOf(song_time)
				lrctime.splice(a, 1)
				$(".l_lrctitel").map(function(e) {
					$(".l_lrctitel").eq(e).css({
						"font-weight": 500,
						color: "gray",
					})
				})
				$(".l_lrctitel").eq(a - 1).css({
					color: "white",
					"font-weight": 700
				})
				var length = $(".l_lrctitel").length
				if(a > 10 && a <= length - 10) {
					$(".l_lrc").stop(true)
					$(".l_lrc").animate({
						scrollTop: (a - 10) * 20
					}, 500)
				}
				playing = ((song_time / songTime) * 100).toFixed(2)
				if($(".ranges")[0]) {
					$(".ranges")[0].value = playing
				}
				$scope.l_playTime = Math.floor(song_time / 60) + ":" +
					((song_time % 60).toFixed(0).toString().length < 2 ? "0" + (song_time % 60).toFixed(0) : (song_time % 60).toFixed(0))
				$scope.$digest();
			})
		})
		$scope.l_gotoLove = function() {
			l_userdata = localStorage.getItem("myLike")
			var l_like_name = localStorage.getItem("myLike_name")
			var l_like_author =	localStorage.getItem("myLike_author")
			if(l_userdata) {
				l_userdata = JSON.parse(l_userdata)
				l_like_author = JSON.parse(l_like_author)
				l_like_name = JSON.parse(l_like_name)
			} else {
				l_userdata = []
				l_like_name = []
				l_like_author = []
			}
			if($scope.showHeat == true) {
				$scope.showHeat = false
				var a = l_userdata.indexOf($scope.id)
				l_userdata.splice(a, 1)
				l_like_author.splice(a, 1)
				l_like_name.splice(a, 1)
				l_userdata = JSON.stringify(l_userdata)
				l_like_name = JSON.stringify(l_like_name)
				l_like_author = JSON.stringify(l_like_author)
				localStorage.setItem("myLike", l_userdata)
				localStorage.setItem("myLike_name", l_like_name)
				localStorage.setItem("myLike_author", l_like_author)
			} else {
				$scope.showHeat = true
				l_userdata.push($scope.id)
				l_like_name.push($scope.l_name)
				l_like_author.push($scope.l_author)
				l_like_name = JSON.stringify(l_like_name)
				l_like_author = JSON.stringify(l_like_author)
				l_userdata = JSON.stringify(l_userdata)
				localStorage.setItem("myLike", l_userdata)
				localStorage.setItem("myLike_name", l_like_name)
				localStorage.setItem("myLike_author", l_like_author)
			}
			
		}
		
	})
	.factory('songInfo', function() { //请求服务		

		return {

		}
	})